﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Security.Cryptography;

namespace LANmine
{
    abstract class Login
    {
        /**
         * This method queries the database against a given username and password,
         * fills up the given User object accordingly and returns true/false.
         * @return boolean
         * @exception SqlException
         */
        public static Boolean AuthenticateUser(String user, String pass)
        {
            // Build the query
            // TODO: Stored Procedure for login
            String loginQry = "SELECT TOP 1 * FROM deelnemers WHERE gebruikersnaam='" + user + "' AND wachtwoord='" + HashCode(pass) + "';";

            try
            {
                /** Set up a Read-Only Database Connection to authenticate user */
                Database myDb = new Database("ro");

                /** Get the SQL connection from the DB object */
                SqlConnection dbConn = myDb.getDbConn();

                /** Open the connection */
                dbConn.Open();

                /** Query the database */
                SqlDataReader dbRead = myDb.getDbReader(loginQry);

                /** Check whether our query returned some values */
                if (dbRead.HasRows)
                {
                    /** Close the database connection */
                    dbConn.Close();
                    return true;
                }
                else
                {
                    /** Close the database connection */
                    dbConn.Close();
                    return false;
                }
            }
            catch (Exception)
            {
                /** Rethrow the exception */
                throw;
            }
        }

        public static Boolean AuthenticateUser(User uObj, String u, String p)
        {
            // Build the query
            // TODO: Stored Procedure for login
            String loginQry = "SELECT TOP 1 * FROM deelnemers WHERE gebruikersnaam='" + u + "' AND wachtwoord='" + HashCode(p) + "';";

            try
            {
                /** Set up a Read-Only Database Connection to authenticate user */
                Database myDb = new Database("ro");

                /** Get the SQL connection from the DB object */
                SqlConnection dbConn = myDb.getDbConn();

                /** Open the connection */
                dbConn.Open();

                /** Query the database */
                SqlDataReader dbRead = myDb.getDbReader(loginQry);

                /** Check whether our query returned some values */
                if (dbRead.Read())
                {
                    /** Fill up the User Object */
                    uObj.ID = Convert.ToUInt32(dbRead["id"].ToString());
                    uObj.UserName = dbRead["gebruikersnaam"].ToString();
                    uObj.LastName = dbRead["naam"].ToString();
                    uObj.FirstName = dbRead["voornaam"].ToString();
                    uObj.Paid = Convert.ToBoolean(dbRead["betaald"].ToString());
                    uObj.Credits = Convert.ToUInt32(dbRead["credits"].ToString());
                    uObj.IsStaff = Convert.ToBoolean(dbRead["staff"].ToString());
                    uObj.Language = dbRead["taal"].ToString();
                    uObj.Email = dbRead["email"].ToString();

                    /** Close the database connection */
                    dbConn.Close();
                    return true;
                }
                else
                {
                    /** Close the database connection */
                    dbConn.Close();
                    return false;
                }
            }
            catch (Exception)
            {
                /** Rethrow the exception */
                throw;
            }
        }

        /**
         * Generate a SHA1 hash from a string
         */
        public static String HashCode(String str)
        {
            SHA1 hash = SHA1.Create();
            ASCIIEncoding encoder = new ASCIIEncoding();

            byte[] bytestr = encoder.GetBytes(str);

            return Convert.ToBase64String(hash.ComputeHash(bytestr));
        }  
    }
}
